package cn.com.surker.production.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.production.invoice.ShipTypeEnum;
import cn.com.surker.production.entity.InvoiceHandle;
import cn.com.surker.production.service.ErpInvoiceHandleService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author luotao
 * @date 2024/9/26
 */
@Service
public class ErpInvoiceHandleImpl extends InvoiceHandleBaseImpl implements ErpInvoiceHandleService {

	@Override
	public List<InvoiceHandle> findSourceInvoice(Long orgId, Long invoiceId, String startTime, String endTime, Integer... shipTypes) {
		if (orgId == null || invoiceId == null) {
			//	源发货单为空
			return null;
		}
		if (shipTypes == null) {
			//	发货单的操作类型为空
			return null;
		}
		QueryWrapper<InvoiceHandle> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("source_org_id", orgId);
		qw.eq("source_invoice_id", invoiceId);
		qw.in("operate_type", shipTypes);
		if (StringUtils.isNotBlank(startTime)) {
			qw.gt("create_time", startTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			qw.le("create_time", endTime);
		}
		List<InvoiceHandle> backOperates = backOperateDao.selectList(qw);
		return backOperates;
	}

	@Override
	public InvoiceHandle queryBackByInvId(Long orgId, Long invoiceId) {
		QueryWrapper<InvoiceHandle> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("source_org_id", orgId);
		qw.eq("source_invoice_id", invoiceId);
		qw.eq("operate_type", ShipTypeEnum.BACK_SCRAP_WEIGHT.getCode());
		qw.last("limit 1");
		InvoiceHandle invoiceHandle = backOperateDao.selectOne(qw);
		return invoiceHandle;
	}
}
